
#import "LaunchImageTransition.h"
#import "InfomationViewController.h"
#import "TabbarViewController.h"
#import "ICSDrawerController.h"
#import "AddressBookLeftViewController.h"
#import "TouristViewController.h"
#import "HomePageController.h"
#import "LoginMainViewController.h"

@implementation LaunchImageTransition

- (void)dealloc{
}

-(id)init{
    if ((self = [super init])) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(timerFireMethod:)
                                                     name:@"userLogin"
                                                   object:nil];
        
    }
    return self;
}

-(void)loadView{
    [super loadView];
    
    [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
    
    
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}


- (void)timerFireMethod:(NSTimer *)theTimer {
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL blInfo = [[defaults objectForKey:@"isNoNeedInfo"] boolValue];
    
    if (blInfo  == NO) {
        //默认值是NO 需要启动画面
        InfomationViewController *info = [[InfomationViewController alloc] init];
        [self.navigationController pushViewController:info animated:YES];
        
    }else{
        //这里本来是用nickName做判断的，发现接口loginCheckForDemo返回nickName为空，所以用LOGIN_USER_REALID做判断
        NSString *sUserrealid = [DE getGlobalVar:LOGIN_USER_REALID];
        //保存了登录记录
        NSLog(@"登陆账号为sUserrealid：%@",sUserrealid);
        if (sUserrealid && sUserrealid.length>0) {
            //检查是否更新库表操作
            if(![self CheckUpdateBase]) //如果更新数据库不走主流程了
            {
                AddressBookLeftViewController *colorsVC = [[AddressBookLeftViewController alloc] init];
                
                TabbarViewController *tabView = [[TabbarViewController alloc] init];
                ICSDrawerController *drawer = [[ICSDrawerController alloc] initWithLeftViewController:colorsVC
                                                                                 centerViewController:tabView];
                kAppDelegate.iCSDrawerController = drawer;
                [self.navigationController setViewControllers:@[drawer] animated:YES];
            }
        }else{
            
            TouristViewController *info = [[TouristViewController alloc] init];
            [self.navigationController pushViewController:info animated:YES];
            
        }
        
    }
}

-(BOOL) CheckUpdateBase{
    NSString *dataVersion = [DE getGlobalVar:[NSString stringWithFormat:@"%@_%@", [DE getGlobalVar:LOGIN_USER_REALID],DATABASEVERSION]];
    NSLog(@"databasevision.......%@",dataVersion);
    //是否为当前版本号，不是的话，需要更新数据库
    
    if (![dataVersion isEqualToString:CURDATABASEVERSION]) {
        NSLog(@"需要更新数据库");
        //更新数据库版本号
        [DE setGlobalVar:[NSString stringWithFormat:@"%@_%@", [DE getGlobalVar:LOGIN_USER_REALID],DATABASEVERSION] value:CURDATABASEVERSION];
        [self UpdateDataBase];
        return  YES;
    }
    return NO;
}

-(void)UpdateDataBase{
    //先删除所有表，再重建所有表，清空用户名和密码，强制用户重新登录，最后设置版本号为当前版本号
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    NSLog(@"创建表的位置：%@",DE.dataPath);
    //contacts
    NSString *sdelsql = @"DROP  TABLE IF EXISTS 'contacts'";
    NSString *createStr=@"CREATE  TABLE  IF NOT EXISTS 'contacts' ('PK_ID' text PRIMARY KEY  NOT NULL  UNIQUE ,'ID' text,'NAME' text, 'PROF_NAME' text,'CLASS_NAME' text,'NJ' text,'HEAD_IMG' text,'SEX' text,'QP' text,'REF_ID' text,'TYPE' text,'DEPT' text,'OPERATE' text,'IS_ACTIVATED' text,'CREATE_USER' text ,'ORG_TYPE' text,'HAS_CHILD' text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    [_dataBase executeUpdate:sdelsql];
    BOOL worked = [_dataBase executeUpdate:createStr];
    FMDBQuickCheck(worked);
    
    //datadict
    sdelsql = @"DROP  TABLE  IF EXISTS 'datadict'";
    createStr=@"CREATE  TABLE  IF NOT EXISTS 'datadict' ('dictdataId' text PRIMARY KEY  NOT NULL  UNIQUE ,'dictdataName' text,'dictdataOrder' text, 'dictdataValue' text,'dicttypeId' text,'dictdataDesc' text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    [_dataBase executeUpdate:sdelsql];
    worked = [_dataBase executeUpdate:createStr];
    FMDBQuickCheck(worked);
    
    //keybooks
    sdelsql = @"DROP  TABLE IF EXISTS 'keybooks'";
    createStr=@"CREATE  TABLE  IF NOT EXISTS 'keybooks' ('kid' text PRIMARY KEY  NOT NULL  UNIQUE ,'words'  text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    [_dataBase executeUpdate:sdelsql];
    worked = [_dataBase executeUpdate:createStr];
    FMDBQuickCheck(worked);
    
    //message
    sdelsql = @"DROP  TABLE IF EXISTS 'message'";
    createStr=@"CREATE  TABLE  IF NOT EXISTS 'message' ('id' text PRIMARY KEY  NOT NULL  UNIQUE ,'receiverId' text,'sequences' text, 'user_id' text,'senderId' text,'messageType' text,'mediaType' text,'state' text,'length' text,'mesSesion_sess_id' text ,'ut_id' text,'text' text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    [_dataBase executeUpdate:sdelsql];
    worked = [_dataBase executeUpdate:createStr];
    FMDBQuickCheck(worked);
    
    //messagesession
    sdelsql = @"DROP  TABLE  IF EXISTS  'messagesession'";
    createStr=@"CREATE  TABLE  IF NOT EXISTS 'messagesession' ('sess_id' text PRIMARY KEY  NOT NULL  UNIQUE ,'sessionPic' text,'text' text, 'title' text,'badgeNumber' int,'sessionType' text,'oppositeId' text,'isremind' text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    [_dataBase executeUpdate:sdelsql];
    worked = [_dataBase executeUpdate:createStr];
    FMDBQuickCheck(worked);
    
    //publicaccount
    sdelsql = @"DROP  TABLE  IF EXISTS 'publicaccount'";
    createStr=@"CREATE  TABLE  IF NOT EXISTS 'publicaccount' ('appId' text PRIMARY KEY  NOT NULL  UNIQUE ,'appName' text,'type' text, 'sort' text,'qp' text,'initialWord' text,'funcDesc' text,'headImg' text,'subscribeStatus' text,'isRecommend' text,'isReceive' text,'disableChat' text,'OTHER_INFO' text,'UPDATE_TIME' date,'isBasic' text,'isBanner' text,'appType' text,'isHome' text,'userSort' text)";
    [_dataBase executeUpdate:sdelsql];
    worked = [_dataBase executeUpdate:createStr];
    FMDBQuickCheck(worked);
    
    //TODO 当前用户通讯录版本号设置为空，公众号版本号设置为空，关键字版本号设置为空
    //最后 才把userid设置为空  //[DE setUserId:@""];
    //清空用户通讯录版本号
    // [DE setGlobalVar:[NSString stringWithFormat:@"%@_%@", [DE getGlobalVar:LOGIN_USER_REALID],[DE getGlobalVar:CONTACT_VERSION]] value:txlVersion];
    
    [DE setGlobalVar:[NSString stringWithFormat:@"%@_%@", [DE getGlobalVar:LOGIN_USER_REALID],[DE getGlobalVar:CONTACT_VERSION]] value:@""];
    
    NSString *sversion = [DE getGlobalVar:[NSString stringWithFormat:@"%@_%@", [DE getGlobalVar:LOGIN_USER_REALID],[DE getGlobalVar:CONTACT_VERSION]]];
    
    NSLog(@"GENGXINHOU %@",sversion);
    
    //清空关键字版本号：
    [DE setGlobalVar:[NSString stringWithFormat:@"%@_%@",DE.userId,@"keyBookVersion"] value:@""];
    //清空用户ID
    [DE setUserId:@""];
    
    //回到登陆页面重新登陆
    LoginMainViewController *lgoin = [[LoginMainViewController alloc] init];
    lgoin.TypeLoginMain = @"1";//
    [self.navigationController setViewControllers:@[lgoin]];
}


@end